package org.vectrics.web.struts;

import java.util.*;
import org.apache.log4j.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import org.vectrics.company.Company;
import org.vectrics.company.CompanySelectCriteria;
import org.vectrics.provider.CoreServiceLocator;


public class CompanyComboTag extends TagSupport {
	public static final long serialVersionUID = 8021971987432L;
    static private Category log = Category.getInstance(CompanyComboTag.class);
    private String styleClass = null;
    private String property = null;
    private String selectedId = null;

    public CompanyComboTag() {
    }

    private String getPathCompanyName(Company company) {
    	String name = company.getName();
  	
    	Company parent = null;
    	parent = company.getParent();
    	
    	if (parent == null) {
    		name = company.getName();
    	}
    	else {
    		name = parent.getName() + " - " + company.getName();
    	}
    	return(name);
    }
    
	public int doStartTag() throws JspException  {
		log.debug("doStartTag() - render a combo box with all companies listed");
		StringBuffer html = new StringBuffer();
		
		log.debug("doStartTag() called");
		HttpServletRequest request = (HttpServletRequest)super.pageContext.getRequest();
		
		//HttpSession session = super.pageContext.getSession();
		if (styleClass == null) {
			html.append("<select name=\"" + getProperty() +  "\" style=\"width:250;font-family:verdana,arial,sans serif;font-size=11px\">");
		}
		else {
			html.append("<select name=\"" + getProperty() +  "\" class=\"" + styleClass + "\">");
		}
		
		
		log.debug("Iterate through all company ids, adding each company to the combo");
		html.append("<option value=\"" + "" + "\">" 
				+ "" + "</option>");

		CompanySelectCriteria criteria = new CompanySelectCriteria();
		criteria.setVoidedType(CompanySelectCriteria.VOID_NO);
		Iterator iter = CoreServiceLocator.getCompanyService().findCompanies(criteria, 0, 100).iterator();
		while (iter.hasNext()) {
			String selectedStr = "";
			Company company = (Company)iter.next();
			Long id = company.getId();
			if (getSelectedId() != null) {
				if (id.equals(new Long(getSelectedId()))) {
					selectedStr = " SELECTED";
				}
			}
			try {
				if ((company != null) && ((company.getVoided()) || (company.getId().equals(this.selectedId)))) {
					try {
						String companyName = this.getPathCompanyName(company);
						html.append("<option value=\"" + company.getId() + "\"" + selectedStr + ">" 
								+ companyName + "</option>");
					} catch (Throwable ex) {
						log.error("Exception finding company name (might be problem with company parent" + ex.getMessage());
					}
				}
			}
			catch (Throwable t) {
				log.warn("Exception looking up company id = " + id);
			}
		}		
		html.append("</select>");

		
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(html.toString());
		}
		catch (IOException ex) {
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		return(EVAL_PAGE);
	}


	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return
	 */
	public String getSelectedId() {
		return selectedId;
	}

	/**
	 * @param selectedId
	 */
	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

}
